package com.bzsg.rightsManagementSystem.controller


import com.alibaba.fastjson2.JSON
import com.bzsg.rightsManagementSystem.pojo.Permission
import com.bzsg.rightsManagementSystem.service.PermissionService
import com.bzsg.rightsManagementSystem.service.RequestPathService
import com.bzsg.rightsManagementSystem.util.result.ErrorType
import com.bzsg.rightsManagementSystem.util.result.RequestResult
import com.bzsg.rightsManagementSystem.util.result.ResultUtil
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*

@Controller
class PermissionController {
    private val logger = LoggerFactory.getLogger(this.javaClass)

    @Autowired
    lateinit var permissionService: PermissionService

    @Autowired
    lateinit var requestPathService: RequestPathService

    @PostMapping(value = ["api/addPermission"])
    @ResponseBody
    fun addPermission(@RequestBody permission: Permission): RequestResult<*> {
        return try {
            permissionService.insert(permission)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/deletePermissionById"])
    @ResponseBody
    fun deletePermission(@RequestParam permissionId: Int): RequestResult<*> {
        return try {
            permissionService.deleteById(permissionId)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/updatePermission"])
    @ResponseBody
    fun updatePermission(@RequestBody permission: Permission): RequestResult<*> {
        return try {
            permissionService.update(permission)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/queryPermission"])
    @ResponseBody
    fun queryPermission(): RequestResult<*> {
        return try {
            val permissions = permissionService.queryAll()
            ResultUtil.success(permissions)
        } catch (e: Exception) {
            logger.error(e.toString())
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/selectTotalPermission"])
    @ResponseBody
    fun selectTotalPermission(): RequestResult<*> {
        return try {
            ResultUtil.success(permissionService.selectTotalPermission())
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/queryAllPermissionByLimit"])
    @ResponseBody
    fun queryAllByLimit(@RequestParam pageNow: Int, @RequestParam pageSize: Int): RequestResult<*> {
        return try {
            ResultUtil.success(permissionService.queryAllByLimit(pageNow, pageSize))
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/getPathsByPermission"])
    @ResponseBody
    fun getPathsPermission(@RequestParam permissionId: Int): RequestResult<*> {
        val requestPaths = requestPathService.selectPathByPermissionId(permissionId)
        return ResultUtil.success(requestPaths)
    }

    @GetMapping(value = ["api/selectPathsByPermissionId"])
    @ResponseBody
    fun selectPathsByPermissionId(@RequestParam permissionId: Int): RequestResult<*> {
        return try {
            val requestPaths = requestPathService.selectPathByPermissionId(permissionId)
            ResultUtil.success(requestPaths)
        } catch (e: Exception) {
            logger.error(e.toString())
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/updatePermissionPathRelation"])
    @ResponseBody
    fun updatePermissionPathRelation(
        @RequestBody requestPathIds: String?,
        @RequestParam permissionId: Int
    ): RequestResult<*> {
        return try {
            val json = JSON.parseObject(requestPathIds)
            val list = json.get("requestPathIds") as List<Int>
            permissionService.deleteAllPermissionPathRelationByPermissionId(permissionId)
            for (requestPathId in list) {
                permissionService.addPermissionPathRelation(permissionId, requestPathId)
            }
            ResultUtil.success()
        } catch (e: Exception) {
            logger.error(e.toString())
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }
}